package cn.ojh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;

import cn.ojh.domain.WechatButton;
import cn.ojh.mapper.WechatButtonMapper;
import cn.ojh.service.IWechatButtonService;
import cn.ojh.utils.AccessTokenUtil;
import cn.ojh.utils.UrlUtil;

@Service
public class WechatButtonServiceImpl implements IWechatButtonService{

	@Autowired
	private WechatButtonMapper wechatButtonMapper;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private AccessTokenUtil accessTokenUtil;
	
	@Override
	public List<WechatButton> getAll() {
		// TODO Auto-generated method stub
		return wechatButtonMapper.selectAll();
	}

	@Override
	public List<WechatButton> getChildButtonList(Long buttonId) {
		// TODO Auto-generated method stub
		return wechatButtonMapper.getChildButtonList(buttonId);
	}

	@Override
	public void save(WechatButton wechatButton) {
		// 判断是否有主键id，如果没有主键id，则执行新增操作
		if(wechatButton.getId() == null) {
			wechatButtonMapper.insert(wechatButton);
		}
		//否则，执行编辑操作
		else {
			wechatButtonMapper.updateByPrimaryKey(wechatButton);
		}
		
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		wechatButtonMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateButton() {
		// TODO Auto-generated method stub
		List<WechatButton> wechatButtonList = wechatButtonMapper.getChildButtonList(1L);
		Map<String,Object> buttonMap = new HashMap<>();
		buttonMap.put("button",wechatButtonList);
		String buttonJSon = JSON.toJSONString(buttonMap);
		System.out.println(buttonJSon);
		String result = restTemplate.postForObject(UrlUtil.MENU_CREATE_URL.replace("ACCESS_TOKEN", accessTokenUtil.getAccessToken()), buttonJSon, String.class);
		System.out.println(result);
	}

}







